import pandas as pd


def error11_idf(df,dict_info_setting, columns):
    """
    处理健康建议相关字段的逻辑错误，并记录样式和批注信息

    :param df: 数据 DataFrame
    :param columns: 包含10个列名的列表，顺序为 [
        '吸烟量', '建议日吸烟量(支/天)',
        '日饮酒量(两)', '建议日饮酒量(两/天)',
        '每周运动次数(次)', '每次运动时间(分钟/次)',
        '建议每周运动次数(次)', '建议每次运动时间(分钟/次)',
        '主食类(克/天)', '建议主食类(克/天)'
    ]
    :return: (styles_info, comments_info) 样式和批注信息
    """
    styles_info = {}
    comments_info = []

    food_level = [int(info.strip()) for info in dict_info_setting["error11_idf"].split(":")[-1].strip().split(",")]

    # 解包列名
    (col_smoke, col_smoke_adv,
     col_drink, col_drink_adv,
     col_exercise_cnt, col_exercise_time,
     col_exercise_cnt_adv, col_exercise_time_adv,
     col_food, col_food_adv) = columns

    for row_idx, row in df.iterrows():
        # ================= 主食类校验 =================
        food_adv = row[col_food_adv]
        if pd.notna(food_adv):
            # 建议主食量超限校验
            if food_adv > food_level[0] or food_adv < food_level[1]:
                styles_info[(row_idx, col_food_adv)] = 'error'  # 使用红色错误标记
                msg = f"建议主食量({food_adv}克)超出推荐范围({food_level[1]}-{food_level[0]}克)"
                comments_info.append((row_idx, col_food_adv, msg))

            # 实际<建议量校验
            food = row[col_food]
            if pd.notna(food) and food < food_adv:
                styles_info[(row_idx, col_food)] = 'warning'
                styles_info[(row_idx, col_food_adv)] = 'warning'
                msg = f"实际摄入量({food}克)低于建议量({food_adv}克)"
                comments_info.extend([
                    (row_idx, col_food, msg),
                    (row_idx, col_food_adv, msg)
                ])

        # ================= 吸烟校验 =================
        smoke = row[col_smoke]
        smoke_adv = row[col_smoke_adv]
        if pd.notna(smoke) and pd.notna(smoke_adv):
            if 0 < smoke <= smoke_adv:
                styles_info[(row_idx, col_smoke)] = 'warning'
                styles_info[(row_idx, col_smoke_adv)] = 'warning'
                msg = f"实际吸烟量({smoke}支)未超过建议量({smoke_adv}支)"
                comments_info.extend([
                    (row_idx, col_smoke, msg),
                    (row_idx, col_smoke_adv, msg)
                ])

        # ================= 饮酒校验 =================
        drink = row[col_drink]
        drink_adv = row[col_drink_adv]
        if pd.notna(drink) and pd.notna(drink_adv):
            if 0 < drink <= drink_adv:
                styles_info[(row_idx, col_drink)] = 'warning'
                styles_info[(row_idx, col_drink_adv)] = 'warning'
                msg = f"实际饮酒量({drink}两)未超过建议量({drink_adv}两)"
                comments_info.extend([
                    (row_idx, col_drink, msg),
                    (row_idx, col_drink_adv, msg)
                ])

        # ================= 运动校验 =================
        ex_cnt = row[col_exercise_cnt]
        ex_time = row[col_exercise_time]
        ex_cnt_adv = row[col_exercise_cnt_adv]
        ex_time_adv = row[col_exercise_time_adv]

        # 实际运动量计算
        if all(pd.notna([ex_cnt, ex_time, ex_cnt_adv, ex_time_adv])):
            actual = ex_cnt * ex_time
            suggest = ex_cnt_adv * ex_time_adv
            if actual > suggest:
                for col in [col_exercise_cnt, col_exercise_time,
                            col_exercise_cnt_adv, col_exercise_time_adv]:
                    styles_info[(row_idx, col)] = 'warning'
                msg = f"实际运动量({actual}分钟/周)超过建议量({suggest}分钟/周)"
                comments_info.extend([
                    (row_idx, col_exercise_cnt, msg),
                    (row_idx, col_exercise_time, msg),
                    (row_idx, col_exercise_cnt_adv, msg),
                    (row_idx, col_exercise_time_adv, msg)
                ])

        # 运动次数与时间冲突
        if pd.notna(ex_cnt) and pd.notna(ex_time):
            if (ex_cnt == 0 and ex_time != 0) or (ex_cnt != 0 and ex_time == 0):
                for col in [col_exercise_cnt, col_exercise_time]:
                    styles_info[(row_idx, col)] = 'warning'
                msg = "实际运动次数与时间应同时为0或同时不为0"
                comments_info.extend([
                    (row_idx, col_exercise_cnt, msg),
                    (row_idx, col_exercise_time, msg)
                ])

    return styles_info, comments_info
